package cn.blue.pet.service.impl;

import cn.blue.base.util.BaiduAuditUtils;
import cn.blue.base.util.LoginContext;
import cn.blue.org.domain.Employee;
import cn.blue.pet.domain.*;
import cn.blue.pet.domain.Product;
import cn.blue.pet.domain.ProductOnlineAuditLog;
import cn.blue.pet.mapper.ProductDetailMapper;
import cn.blue.pet.mapper.ProductMapper;
import cn.blue.pet.mapper.ProductOnlineAuditLogMapper;
import cn.blue.pet.service.IProductService;
import cn.blue.base.service.impl.BaseServiceImpl;
import cn.blue.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hui
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断，全部查询出来，只要状态是下架那么就不处理
        productMapper.batchOffsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        // 1.我要过滤掉上架状态的数据，我要把数据转换为map，key就是主键，value就是对象
        Map<Long, Product> productMaps = productMapper.findAll().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId,product -> product));
        Date date = new Date();
        for (Long id : ids) {
            Product product = productMaps.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null,说明数据本身就是下架的
            if (product != null){
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    sb.append("宠物上架百度AI审核名称失败，宠物名称为：" + product.getName() + ";");
                }
                if (product.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + product.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + product.getResources() + ";");
                    }
                }


                if (sb.length() > 0){
                    ProductOnlineAuditLog petOnlineAuditLog = new ProductOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setProductId(id);
                    petOnlineAuditLog.setNote(sb.toString());
                    petOnlineAuditLog.setState(0);
                    productOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                product.setState(1);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        }

    }



    //立即购买
    @Override
    public void byNow(Product product) {

    }

    @Override
    public Product productDetail(Long id) {
        return productMapper.productDetail(id);
    }
    //新增


    @Override
    public void add(Product product) {
        super.add(product);
        ProductDetail detail = product.getDetail();
        if (detail != null){
            detail.setProductId(product.getId());
            productDetailMapper.add(detail);
        }else {
            ProductDetail productDetail = new ProductDetail();
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }

    }
}
